<?php
namespace Admin\Controller;
use Think\Controller;
class courseController extends Controller {

    //显示用户列表方法
    public function courselistJson(){
       
       $use= $this->users=M('course')->select();
   echo json_encode($use);
      } 
  
    public function courseList() {
        $courseModel = M('course');
        $courseList = $courseModel -> select();

        $this -> assign('courseList',$courseList);
        $this -> display();
    }
   public function doAddcourse() {
          $courseModel = M('course');
        //上传图片
        if ($_FILES['coursePic']['error'] == 0) {
            $imageInfo = $this->upload();
            $pname = $imageInfo['coursePic']['savename'];
        } else {
            $pname = "default.jpg";
        }


        $addcourseData['coursename'] = I('post.coursename');
        $addcourseData['address'] = I('post.address');
        $addcourseData['coursedate'] = date('y-m-d H:m:s');
        $addcourseData['coursetext'] = I('post.coursetext');


        $res = $courseModel -> add($addcourseData);

        if ($res) {
            $this -> success("添加成功!");
        } else {
            $this -> error("添加失败!");
        }
    }  
    public function updatecourse($id){
        $courseModel = M('course');
        $courseData['courseid'] = $id;
        $res = $courseModel -> where($courseData) -> find();
        $this -> assign('courseInfo', $res);
        $this -> display();
    }
    public function doupdatecourse() {
        $courseModel = M('course');

        $updatecourseData['courseid'] = I('post.courseid');
        $updatecourseData['coursename'] = I('post.coursename');
        $updatecourseData['address'] = I('post.address');
        $updatecourseData['coursedate'] = date('y-m-d H:m:s');
        $updatecourseData['coursetext'] = I('post.coursetext');

        $res = $courseModel -> save($updatecourseData);

        if ($res) {
            $this -> success("修改成功!",U('course/courseList'));
        } else {
            $this -> error("修改失败!");
        }
    }
       public function delcourse($id){
        $res= M("course") -> where("courseid = '$id'") -> delete();
       if ($res) {
            $this -> success("删除成功!",U('course/courseList'));
        } else {
            $this -> error("修改失败!");
        }
    }

    

    //  public function index(){
    //     $share = M('share');
    //     //用户名条件搜索
    //     if(!empty($_GET['name'])){
    //       $where['name'] = array('like','%'.$_GET['name'].'%');
    //     }
    //     $total=$user->where($where)->count();
        
    //     //实例化分页类
    //     $page=new \Think\Page($total,5);
    //     $this->assign('show',$page->show());
    //     $data=$user->where($where)->page(I('get.p',1),5)->select();
    //     $this->assign('data',$data);
    //     $this->display();
    // }
public function Upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/course/'; // 设置附件上传根目录
      //  $upload->savePath  =   'Banner/'; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload(); // 上传文件

        if (!$info) {
            $this->error($upload->getError());
        } else {
            return $info;
        }
    }
}
